﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using MySql;
using MySql.Data.MySqlClient;

using INSTA_ORM;


namespace INSTA_ORM
{
    public class DataMapper
    {
        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        /// <remarks></remarks>
        public int Id { get; protected set; }

        /// <summary>
        /// Gets or sets the db accessor.
        /// </summary>
        /// <value>The db accessor.</value>
        /// <remarks></remarks>
        DbAccessor DbAccessor { get; set; }

        public ClassMapping ClassMapping; // FIXME : why its not visible from somewhere ELSE ??? { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="DataMapper"/> class.
        /// </summary>
        /// <remarks></remarks>
        public DataMapper() : this(-1)
        {}

        /// <summary>
        /// Initializes a new instance of the <see cref="DataMapper"/> class.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <remarks></remarks>
        public DataMapper(int id)
        {
            Id = id;
            ClassMapping = MappingInfo.getInstance().findMappingInfosByClassName(this.GetType().Name);
            DbAccessor = new DbAccessor(this);
        }


        /// <summary>
        ///  Permet de charger l'objet à partir de la base de données.
        /// </summary>
        /// <remarks></remarks>
        public void Load()
        {
            DataSet result = DbAccessor.getResultForSelect();
            DataTable dt = result.Tables[0];
            // Eviter IndexOutOfRangeException
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                foreach (var property in ClassMapping.Properties)
                {
                    string varName = property.Value.PropertyName;
                    string dbName = property.Value.PropertyFieldName;
                    ReflectionHelper.SetPropertyValue(varName, this, dr[dbName]);

                }
            }else{
                // FIXME : Code de contrôle lors d'une tentative de chargement d'un element à l'index inexistant
            }
        }

        /// <summary>
        /// Permet de sauvegarder ou de mettre à jour l'objet dans la base de données.
        /// </summary>
        /// <returns></returns>
        public int Save()
        {
            int result = 0;
            if (Id == -1)
            {
                result = DbAccessor.getResultForInsert();
            }
            else if (Id > 0)
            {
                result = DbAccessor.getResultForUpdate();
            }

            return result;        
        }

        /// <summary>
        /// Permet de supprimer l'objet de base de données.
        /// </summary>
        /// <returns></returns>
        public int Delete()
        {
            int result = DbAccessor.getResultForDelete();
            return result;
        }
    }
}
